using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;

using SLIITPro.Core;
using SLIITPro.Data.Entity;

namespace SLIITPro.Data.Extension
{
    public class BusinessItemExtension
    {
        private Datamanager _datamanager;
        public BusinessItemExtension()
        {
            _datamanager = new Datamanager();
        }

               
        public void CreateBusinessItem(BusinessItem item)
        {
            var query = Properties.Resources.queryCreateBusiness;
            var parameters = new SqlParameter[12];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = item.BUId };
            parameters[1] = new SqlParameter("@2", SqlDbType.NVarChar) { Value = item.BName };
            parameters[2] = new SqlParameter("@3", SqlDbType.NVarChar) { Value = item.BStreet };
            parameters[3] = new SqlParameter("@4", SqlDbType.NVarChar) { Value = item.BCity };
            parameters[4] = new SqlParameter("@5", SqlDbType.NVarChar) { Value = item.BCountry };
            parameters[5] = new SqlParameter("@6", SqlDbType.NVarChar) { Value = item.BPhone };
            parameters[6] = new SqlParameter("@7", SqlDbType.NVarChar) { Value = item.BWeb };
            parameters[7] = new SqlParameter("@8", SqlDbType.NVarChar) { Value = item.BEmail };
            parameters[8] = new SqlParameter("@9", SqlDbType.NVarChar) { Value = item.BType };
            parameters[9] = new SqlParameter("@10", SqlDbType.NVarChar) { Value = item.BCreatedAt };
            parameters[10] = new SqlParameter("@11", SqlDbType.NVarChar) { Value = item.BUpdatedAt };
            parameters[11] = new SqlParameter("@12", SqlDbType.NVarChar) { Value = item.BImageFolderID };
            _datamanager.ExecuteNonQuery(query, parameters);
        }

        public void UpdateBusinessRating(BusinessItem item)
        {
            var query = Properties.Resources.queryUpdateBusinessRating;
            var parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = item.BRater };
            parameters[1] = new SqlParameter("@2", SqlDbType.Float) { Value = item.BRating };
            parameters[2] = new SqlParameter("@3", SqlDbType.Int) { Value = item.BId };
            _datamanager.ExecuteNonQuery(query, parameters);
        }

        public void DeleteBusinessItem(int id)
        {
            var query = Properties.Resources.queryDeleteBusiness;
            var parameter = new SqlParameter[1];
            parameter[0] = new SqlParameter("@1", SqlDbType.Int) { Value = id };
            _datamanager.ExecuteNonQuery(query, parameter);
        }

        public DataTable SelectBusiness(int bId)
        {
            var query = Properties.Resources.querySelectBusinessById;
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@1", SqlDbType.Int) { Value = bId };
            return _datamanager.ExecuteQuery(query, "BusinessItem", parameters);
        }

        public List<BusinessItem> SelectBusiness()
        {
            var query = SLIITPro.Data.Properties.Resources.querySelectBusiness;
            var table = _datamanager.ExecuteQuery(query, "BusinessItem", null);
            var business = (from DataRow row in table.Rows
                            select new BusinessItem(row)).ToList();
            return business;
        }

        public List<string> GetBusinessList()
        {
            var query = Properties.Resources.queryGetBusinessList;
            var names = new List<string>();
            var table = _datamanager.ExecuteQuery(query, "BusinessItem", null);
            if (table.Rows.Count != 0)
            {
                names.AddRange(from DataRow row in table.Rows
                               select (string)row[0]);
            }

            return names;
        }

        public void DeleteImageFolder(string path)
        {
            if (Directory.Exists(path))
                Directory.Delete(path, true);
        }
    }
}